import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '*',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,

    },
    {
        path: '/about',
        name: 'About',
        component: () =>
            import ('../views/About.vue'),
        redirect: '/welcome',
        children: [{
                path: '/welcome',
                name: 'Welcome',
                component: () =>
                    import ('../views/Welcome.vue'),
                // meta: {
                //   loginFlag: true //给谁守卫
                // },
            }, {
                path: '/users',
                name: 'Users',
                component: () =>
                    import ('../views/consumer/Users.vue'),
            }, {
                path: '/roles',
                name: 'Roles',
                component: () =>
                    import ('../views/power/Roles.vue'),
            }, {
                path: '/rights',
                name: 'Rights',
                component: () =>
                    import ('../views/power/Rights.vue'),
            }, {
                path: '/goods',
                name: 'Goods',
                component: () =>
                    import ('../views/wares/Goods.vue'),
            },
            {
                path: '/goods/add',
                name: 'Add',
                component: () =>
                    import ('../views/wares/add.vue'),
            }, {
                path: '/params',
                name: 'Params',
                component: () =>
                    import ('../views/wares/Params.vue'),
            }, {
                path: '/categories',
                name: 'Categories',
                component: () =>
                    import ('../views/wares/Categories.vue'),
            }, {
                path: '/orders',
                name: 'Orders',
                component: () =>
                    import ('../views/order/Orders.vue'),
            }, {
                path: '/reports',
                name: 'Reports',
                component: () =>
                    import ('../views/report/Reports.vue'),
            }, {
                path: '/orders/demo',
                name: 'demo',
                component: () =>
                    import ('../views/order/demo.vue'),
            }

        ]
    }
]





const router = new VueRouter({
    routes
})


// router.beforeEach((to, from, next) => {
//   if (to.meta.loginFlag) {
//     if (localStorage.getItem('token')) {
//       next()
//     } else {
//       next('/login')
//     }
//   } else {
//     next()
//   }
//   if (to.path == '/login') {//模拟 自动 过期时间
//     localStorage.removeItem('token')
//   }
// })


router.beforeEach((to, from, next) => {
    if (to.path != '/login') {
        if (localStorage.getItem('token')) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }

    if (to.path == '/login') {
        localStorage.removeItem('token') // 模拟 自动 过期时间
    }

})


export default router